package com.xinke.module.system.controller.admin.dict;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.dict.*;
import com.xinke.module.system.model.entity.dict.DictTypeDO;
import com.xinke.module.system.service.dict.DictTypeService;

@Tag(name = "管理后台 - 字典类型")
@RestController
@RequestMapping("/system/dict-type")
@Validated
public class DictTypeController {

    @Resource
    private DictTypeService dictTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建字典类型")
    @PreAuthorize("@ss.hasPermission('system:dict-type:create')")
    public Result<Long> createDictType(@Valid @RequestBody DictTypeSaveReqVO createReqVO) {
        return success(dictTypeService.createDictType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新字典类型")
    @PreAuthorize("@ss.hasPermission('system:dict-type:update')")
    public Result<Boolean> updateDictType(@Valid @RequestBody DictTypeSaveReqVO updateReqVO) {
        dictTypeService.updateDictType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字典类型")
    @Parameter(name = "dtId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:dict-type:delete')")
    public Result<Boolean> deleteDictType(@RequestParam("dtId") Long dtId) {
        dictTypeService.deleteDictType(dtId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得字典类型")
    @Parameter(name = "dtId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dict-type:query')")
    public Result<DictTypeRespVO> getDictType(@RequestParam("dtId") Long dtId) {
        DictTypeDO dictType = dictTypeService.getDictType(dtId);
        return success(BeanUtils.toBean(dictType, DictTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得字典类型分页")
    @PreAuthorize("@ss.hasPermission('system:dict-type:query')")
    public Result<PageResult<DictTypeRespVO>> getDictTypePage(@Valid DictTypePageReqVO pageReqVO) {
        PageResult<DictTypeDO> pageResult = dictTypeService.getDictTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出字典类型 Excel")
    @PreAuthorize("@ss.hasPermission('system:dict-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictTypeExcel(@Valid DictTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictTypeDO> list = dictTypeService.getDictTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "字典类型.xls", "数据", DictTypeRespVO.class,
                        BeanUtils.toBean(list, DictTypeRespVO.class));
    }

}